package com.student.wym.controller;

import com.student.wym.po.Question;
import com.student.wym.po.Student;
import com.student.wym.service.IQuestionService;
import com.student.wym.util.JSONResult;
import com.student.wym.util.PageList;
import com.student.wym.vo.UpdateRequestVo;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import java.io.IOException;
import java.util.Date;
import java.util.List;

@Controller
@RequestMapping("/question")
public class QuestionController {

    @Autowired
    private IQuestionService questionService;

    @RequestMapping(value = "/goPage", method = RequestMethod.GET)
    public String goPage() {
        return "list_question";
    }

    @RequestMapping(value = "/list", method = RequestMethod.POST)
    @ResponseBody
    public PageList list(HttpServletRequest request) {
        /**
         * 不考虑exam_i，直接返回所有试题
         */
        List<Question> questions = questionService.findAll();
        PageList resultList = new PageList();
        resultList.setRows(questions);
        resultList.setTotal(questions.size());
        return resultList;
    }

    @RequestMapping(value = "/upload", method = RequestMethod.POST)
    @ResponseBody
    public JSONResult upload(@RequestParam("file") MultipartFile file) {
        if (file.getSize() == 0) {
            return new JSONResult(1, "导入前请先选择文件");
        }
        String suffix = file.getOriginalFilename().substring(file.getOriginalFilename().lastIndexOf("."));
        if (!suffix.equals(".xlsx") && !suffix.equals(".xls")) {
            return new JSONResult(1, "只能导入excel文件");
        }
        //List<Question> questions = new ArrayList<Question>();
        try {
            Workbook wb = null;
            if (suffix.equals(".xlsx")) { //2007
                wb = new XSSFWorkbook(file.getInputStream());
            } else  //2003
                wb = new HSSFWorkbook(file.getInputStream());
            Sheet sheet = wb.getSheetAt(0);
            for (int i = 1; i <= sheet.getLastRowNum(); i++) {
                Row row = sheet.getRow(i); //读取表中的行
                Student student = new Student();
                Question question = new Question();
                question.setIntro(row.getCell(0).getStringCellValue()); //题干
                row.getCell(1).setCellType(Cell.CELL_TYPE_STRING); //预防纯数字错误
                question.setOptionA(row.getCell(1).getStringCellValue()); //A
                row.getCell(2).setCellType(Cell.CELL_TYPE_STRING);
                question.setOptionB(row.getCell(2).getStringCellValue()); //B
                if (row.getCell(3) != null) {
                    row.getCell(3).setCellType(Cell.CELL_TYPE_STRING);
                    question.setOptionC(row.getCell(3).getStringCellValue()); //C
                } else {
                    question.setOptionC("");
                }
                if (row.getCell(4) != null) {
                    row.getCell(4).setCellType(Cell.CELL_TYPE_STRING);
                    question.setOptionD(row.getCell(4).getStringCellValue()); //D
                } else {
                    question.setOptionD("");
                }
                question.setResult(row.getCell(5).getStringCellValue()); //答案
                question.setCreateTime(new Date()); //创建时间
                row.getCell(6).setCellType(Cell.CELL_TYPE_STRING);
                question.setType(Integer.parseInt(row.getCell(6).getStringCellValue())); //试题类型，1单选  2多选 3判断
                question.setState(0); //状态  0启用 1禁用
                //questions.add(question);
                questionService.insert(question);
            }
            // questionService.addAll(questions);
            return new JSONResult(0, "导入成功");
        } catch (IOException e) {
            e.printStackTrace();
            return new JSONResult(1, "导入失败");
        }
    }

    @RequestMapping(value = "/deleteAll", method = RequestMethod.GET)
    @ResponseBody
    private JSONResult deleteAll() {
        questionService.deleteAll();
        return new JSONResult(0, "清空完成");
    }

    @RequestMapping(value = "/updateSteate", method = RequestMethod.GET)
    @ResponseBody
    private JSONResult updateState(UpdateRequestVo vo) {
        Integer updateState = 1 - vo.getQuestionState();
        vo.setQuestionState(updateState);
        try {
            questionService.update(vo);
            return new JSONResult(0, "更改状态成功！");
        } catch (Exception e) {
            e.printStackTrace();
            return new JSONResult(1, "更改状态失败！.");
        }
    }

}
